<?php

namespace common\models\common;

use yii\base\Model;
use yii\web\UploadedFile;

/**
 * 文件上传
 */
class UploadModel extends Model {
    
    /**
     * @var UploadedFile
     */
    public $file;

    public function rules()
    {
        return [
            [['file'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png,jpg,jpeg,gif', 'checkExtensionByMimeType'=>false ],
            
        ];
    }
    
    public function upload() {
        if (empty($this->validate())) {
            return '';
        }
        $path = 'uploads/' . date('Ymd');
        $name = md5($this->file->baseName . rand(10000, 90000));
        $ext = $this->file->extension;
        \common\tools\Util::mkdir($path);
        $this->file->saveAs($path . '/' . $name . '.' . $ext);
        return['url' => '/' .$path . '/' . $name . '.' . $ext, 'path' =>  \Yii::$app->params['IMG_DOMAIN_NAME']. '/' . $path . '/' . $name . '.' . $ext];
    }

}
